package com.cim.web;

import com.cim.config.GlobalConfig;
import com.cim.pojo.Account;
import com.cim.pojo.Course;
import com.cim.pojo.CourseAccount;
import com.cim.pojo.Video;
import com.cim.service.AccountService;
import com.cim.service.CourseAccountService;
import com.cim.service.CourseService;
import com.cim.service.VideoService;
import com.cim.utils.ResponseUtil;
import com.cim.utils.VideoConcatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags="直播相关")
@RestController
@RequestMapping("/api/live")
public class LiveController {
    @Autowired
    CourseService courseService;
    @Autowired
    VideoService videoService;
    @Autowired
    AccountService accountService;
    @Autowired
    CourseAccountService courseAccountService;

    //前端回调 直播结束 保存相关信息 将视频合并 后存储到文件目录 获取md5后保存到数据库
    @ApiOperation("推流结束回调")
    @PostMapping("/pushend")
    public Object pushEnd(@RequestParam @ApiParam("课时ID") String videoId,
                          HttpServletRequest request){

        String role=(String)request.getAttribute(GlobalConfig.TOKEN_ROLE);
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);

        //判断权限
        if(role==null||!role.equals("true")){
            return ResponseUtil.unauthz();
        }
        Account ac=accountService.getAccountByUserName(user);
        if(ac==null){
            return ResponseUtil.unauthz();
        }
        Video video=videoService.getVideo(videoId);
        Course course=courseService.getCourse(video.getCourseId());
        if(video==null||course==null){
            return ResponseUtil.fail(404,"找不到相关数据");
        }
        CourseAccount ca=courseAccountService.findByCA(ac.getUserId(),video.getCourseId());
        if(ca==null){
            return ResponseUtil.unauthz();
        }


        video.setPlayBack(2);
        String currentTime=String.valueOf(System.currentTimeMillis()/1000);
        video.setCreateTime(currentTime);
        course.setReferTime(currentTime);
        String md5FileName=null;
        //合并视频
        try{
            md5FileName= VideoConcatUtil.concat(videoId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
        video.setPlayBack(2);   //设置状态为直播结束
        video.setCreateTime(String.valueOf(System.currentTimeMillis()/1000));   //设置创建时间为当前时间
        video.setVideoAddress(md5FileName); //更新视频地址为录播视频地址
        video.setPushAddress("");   //清空推流地址和推流秘钥
        video.setPushKey("");
        videoService.updateVideo(video);
        return ResponseUtil.ok(video);
    }

}
